package com.itheima.bigevent2.interceptors;

import com.itheima.bigevent2.pojo.Result;
import com.itheima.bigevent2.utils.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.Map;
/*
* 拦截器*/
@Component
public class LoginInterceptor implements HandlerInterceptor {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws Exception
    {
        //令牌验证
        String token=request.getHeader("Authorization");
        //验证token
        try {
            Map<String, Object> claims = JwtUtil.parseToken(token);
            return true;
        }catch (Exception e)
        {
            //本来时可以这么写，但是接口文档要求返回
            response.setStatus(401);
            return false;//验证失败

        }
    }
}
